package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.accesservice.dao.IDaoYkmCenterConfig;
import com.ocom.accesservice.service.YkmCenterConfigService;
import com.ocom.accesservice.vo.ykmcenterconfig.GetCenterConfigVo;
import com.ocom.common.entity.master.EntityYkmCenterConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YkmCenterConfigServiceImpl extends ServiceImpl<IDaoYkmCenterConfig, EntityYkmCenterConfig> implements YkmCenterConfigService {

    @Autowired
    private IDaoYkmCenterConfig ykmCenterConfig;
    /**
     * 获取中台配置信息（getCenterConfig）【WEB】
     *
     * @return*/
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityYkmCenterConfig> GetCenterConfig(Long comId, GetCenterConfigVo getCenterConfigVo) {
        QueryWrapper<EntityYkmCenterConfig> query = Wrappers.query();
        query.eq("com_id",getCenterConfigVo.getComId());
        if (StringUtils.isNotNull(getCenterConfigVo.getRegionId())){
            query.eq("region_id",getCenterConfigVo.getRegionId());
        }
        if (StringUtils.isNotNull(getCenterConfigVo.getAreaId())){
            query.eq("area_id",getCenterConfigVo.getAreaId());
        }
        if (StringUtils.isNotNull(getCenterConfigVo.getStatus())){
            query.eq("status",getCenterConfigVo.getStatus());
        }
        PageHelper.startPage(getCenterConfigVo.getPage(),getCenterConfigVo.getSize());
        return ykmCenterConfig.selectList(query);
    }

    /**
     * 修改中台配置信息（updateCenterConfig）【WEB】
     * */
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void UpdateCenterConfig(Long comId, EntityYkmCenterConfig entityYkmCenterConfig) {
        ykmCenterConfig.updateById(entityYkmCenterConfig);
    }

    /**
     * 新增中台配置信息（addCenterConfig）【WEB】
     * */
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void AddCenterConfig(Long comId, EntityYkmCenterConfig entityYkmCenterConfig) {
        QueryWrapper<EntityYkmCenterConfig> query = Wrappers.query();
        query.eq("com_id",entityYkmCenterConfig.getComId());
        Integer count = this.count(query);
        if(count>=3){
            throw new RuntimeException("添加失败");
        }
        ykmCenterConfig.insert(entityYkmCenterConfig);
    }

    /**
     * 删除中台配置信息（deleteCenterConfig）【WEB】
     * */
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void DeleteCenterConfig(Long comId, List<Long> idList) {
        ykmCenterConfig.deleteBatchIds(idList);
    }


}
